/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.gui.client;

import nfse.gui.util.NetUtil;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.*;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;

public class NoteDialogBox extends Window {

    final TextArea noteTextArea = new TextArea();

    boolean cancelled = false;

    String queryID = "";

    NetFSEClient client = null;

    public NoteDialogBox(NetFSEClient netfseClient) {
        this.client = netfseClient;
        final NoteDialogBox note = this;

        this.setShadow(true);
        this.setFrame(true);
        this.setModal(true);

        this.setIconCls("pac-icon");

        // Set the dialog box's caption.
        FlexTable table = new FlexTable();
        // DialogBox is a SimplePanel, so you have to set it's widget property
        // to
        // whatever you want its contents to be.

        int idx = -1;

        // table.setHTML(++idx, 0, "<b>Note</b>");
        noteTextArea.setVisibleLines(10);
        // noteTextArea.setCharacterWidth(25);
        // table.setWidget(++idx, 0, noteTextArea);

        Button cancelButton = new Button();
        cancelButton.setText("Cancel");
        cancelButton.addListener(new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject obj) {
                cancelled = true;
                note.hide();
            }
        });

        Button okButton = new Button();
        okButton.setText("OK");
        okButton.addListener(new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject obj) {
                cancelled = false;
                note.hide();
                setNote(queryID, noteTextArea.getText().trim());
            }
        });

        // Button resetButton = new Button();
        // resetButton.setText("Reset");
        // resetButton.addListener(new ButtonListenerAdapter() {
        // public void onClick(Button button, EventObject obj) {
        // noteTextArea.setText("");
        // }
        // });

        this.add(noteTextArea);

        // this.addButton(resetButton);
        this.addButton(okButton);
        this.addButton(cancelButton);

    }

    public void setNote(String queryID, String note) {

        QueryServiceAsync NetFSEService = (QueryServiceAsync) GWT.create(QueryService.class);

        ServiceDefTarget endpoint = (ServiceDefTarget) NetFSEService;
        endpoint.setServiceEntryPoint(NetFSEClient.QUERY_URL);

        AsyncCallback callback = new AsyncCallback() {
            public void onFailure(Throwable caught) {
                NetFSEClient.redirect("NetFSEClient.html");
            }

            public void onSuccess(Object result) {
                client.myQueries();
            }
        };

        NetFSEService.setNote(queryID, NetUtil.convertToHex(note), callback);

    }

    public String getNote() {
        return noteTextArea.getText();
    }

    public void setNote(String note) {
        noteTextArea.setText(note);
    }

    public boolean isCancelled() {
        return cancelled;
    }

    public String getQueryID() {
        return queryID;
    }

    public void setQueryID(String queryID) {
        this.queryID = queryID;
    }

}
